package com.cskaoyan.user.service.impl;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.cskaoyan.mall.commons.exception.ValidateException;
import com.cskaoyan.mall.commons.util.jwt.AESUtil;
import com.cskaoyan.user.constants.UserRetCode;
import com.cskaoyan.user.dal.entitys.Member;
import com.cskaoyan.user.dal.entitys.UserVerify;
import com.cskaoyan.user.dal.persistence.MemberMapper;
import com.cskaoyan.user.dal.persistence.UserVerifyMapper;
import com.cskaoyan.user.dto.UserRegisterRequest;
import com.cskaoyan.user.dto.UserRegisterResponse;
import com.cskaoyan.user.service.UserRegisterService;

import tk.mybatis.mapper.entity.Example;

/**
 * @author zyf
 * @since 2022/06/17 17:36
 */
@Service
public class UserRegisterServiceImpl implements UserRegisterService {

    @Autowired
    MemberMapper memberMapper;

    @Autowired
    UserVerifyMapper userVerifyMapper;

    @Autowired
    JavaMailSender javaMailSender;
    @Override
    public UserRegisterResponse register(UserRegisterRequest userRegisterRequest) {
        UserRegisterResponse userRegisterResponse = new UserRegisterResponse();
        // 判空验证

        userRegisterRequest.requestCheck();
        // 判用户名是否重复
        volidUserNameRepeat(userRegisterRequest);

        // 1向用户表插入一条记录
        Member member = new Member();
        member.setUsername(userRegisterRequest.getUserName());
        member.setEmail(userRegisterRequest.getEmail());
        //用户密码加密
        AESUtil aesUtil = new AESUtil(userRegisterRequest.getUserPwd());
        String encrypt = aesUtil.encrypt();
        member.setPassword(encrypt);

        member.setCreated(new Date());
        member.setUpdated(new Date());
        member.setIsVerified("N");
        member.setState(1);
        int effectRow = memberMapper.insertSelective(member);
        if (effectRow!=1){
            userRegisterResponse.setCode(UserRetCode.USER_REGISTER_FAILED.getCode());
            userRegisterResponse.setMsg(UserRetCode.USER_REGISTER_FAILED.getMessage());
            return userRegisterResponse;
        }

        // 2向用户验证表中插入一条记录
        UserVerify userVerify = new UserVerify();
        userVerify.setUsername(member.getUsername());
        //自己设置生成uuid
        String uuidStr=member.getUsername()+UUID.randomUUID();
        AESUtil aesUtil2 = new AESUtil(uuidStr);
        String uuid = aesUtil2.encrypt();

        userVerify.setUuid(uuid);
        userVerify.setRegisterDate(new Date());
        userVerify.setIsExpire("N");
        userVerify.setIsVerify("N");
        int effectRow2 = userVerifyMapper.insertSelective(userVerify);
        if (effectRow2!=1){
            userRegisterResponse.setCode(UserRetCode.USER_REGISTER_FAILED.getCode());
            userRegisterResponse.setMsg(UserRetCode.USER_REGISTER_FAILED.getMessage());
            return userRegisterResponse;
        }


        // 3发送激活邮件

        sendEmail(uuid,userRegisterRequest);



        userRegisterResponse.setCode(UserRetCode.SUCCESS.getCode());
        userRegisterResponse.setMsg(UserRetCode.SUCCESS.getMessage());
        return userRegisterResponse;
    }
    /**
     * 发送用户激活邮件
 * @param null
     * @return
     * @author zyf
     * @since 2022/06/18 9:46
     */
    private void sendEmail(String uuid, UserRegisterRequest registerRequest) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setSubject("CSMALL用户 激活");
        message.setFrom("zyface2022@163.com");
        message.setTo(registerRequest.getEmail());

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("http://localhost:8099/user/verify?uuid=").append(uuid).append("&username=").append(registerRequest.getUserName());
        message.setText(stringBuilder.toString());

        javaMailSender.send(message);

    }

    private void volidUserNameRepeat(UserRegisterRequest userRegisterRequest) {
        Example example = new Example(Member.class);
        example.createCriteria().andEqualTo("username", userRegisterRequest.getUserName());
        List<Member> members = memberMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(members)) {
            throw new ValidateException(UserRetCode.USERNAME_ALREADY_EXISTS.getCode());
        }

    }
}
